#
# Author:	Johan Nestaas
# Date:		06/08/11
#
# Description:
#	Spider moves around, chases shit.
#	Really no point but to use pygame.
#
import os, sys
import pygame
import random
import math
from pygame.locals import *

if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'

def abspos(p1):
	x,y = p1
	x = abs(x)
	y = abs(y)
	return (x,y)
def randpos():
	return (random.randint(50,750),random.randint(50,550))
def addpos(p1,p2):
	x,y = p1
	x2,y2 = p2
	return (x+x2,y+y2)
def diffpos(p1,p2):
	x,y = p1
	x2,y2 = p2
	return (x-x2,y-y2)
def randmove(v = 3, a = math.radians(random.randint(0,359))):
	x = math.cos(a)*v
	y = math.sin(a)*v
	return (x, y)
	
def load_image(name, colorkey=None):
    fullname = name #os.path.join('data', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', name
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()
           
class Cursor(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = load_image('cursor.png', -1)
        
    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.midtop = pos
        #self.rect.move_ip(5, 10)

    def clicked(self, target):
        hitbox = self.rect.inflate(-5, -5)
        return hitbox.colliderect(target.rect)
        
class Spider(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = load_image('spider.gif', -1)
        self.pos = randpos()
        self.vel = 5
        self.avel = math.radians(15)
        self.ang = math.radians(random.randint(0,359))
        self.targ = randpos()
        x,y = diffpos(self.pos, self.targ)
        self.tang = math.pi + math.atan2(y,x)
        self.wobble = 30
        self.targetting = True
        self.hunt = 0
        self.attn = 150
    def update(self):
    	if self.targetting:
    		#self.attn = self.attn - 1
    		if self.attn < 0:
    			self.attn = 150
    			hunt = 1
    		if self.hunt > 0:
    			self.wobble = 0
    			self.vel = 10
    			self.avel = math.radians(30)
    		else:
    			self.vel = 5
    			self.avel = math.radians(15)
    			self.wobble = 30
	    	x,y = diffpos(self.pos, self.targ)
	    	ta = random.randint(-self.wobble, self.wobble)
	    	self.tang = math.pi + math.atan2(y, x) + math.radians(ta)
	    	if self.tang > self.ang:
	    		chg = self.tang - self.ang
	 		if chg > self.avel:
	 			chg = self.avel
	    		self.ang = self.ang + chg
	    	if self.tang < self.ang:
	    		chg = self.ang - self.tang
	 		if chg > self.avel:
	 			chg = self.avel
	    		self.ang = self.ang - chg
	    	x,y = self.pos
	    	x2,y2 = randmove(self.vel, self.ang)
	    	self.pos = (x+x2,y+y2)
	    	if abspos(diffpos(self.targ, self.pos)) < (5,5):
	    		self.targ = randpos()
	    		self.hunt = random.randint(-2,1)
	    		if self.hunt > 0: 
	    			print "Hunting"
        self.rect.midtop = self.pos

    def coll(self, target):
        hitbox = self.rect.inflate(-1, -1)
        return hitbox.colliderect(target.rect)
        
pygame.init()
random.seed()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Test')
#pygame.mouse.set_visible(0)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
if pygame.font:
    font = pygame.font.Font(None, 36)
    text = font.render("Testing", 1, (10, 10, 10))
    textpos = text.get_rect(centerx=background.get_width()/2)
    background.blit(text, textpos)

screen.blit(background, (0, 0))
pygame.display.flip()
cursor = Cursor()
spider = Spider()
allsprites = pygame.sprite.RenderPlain((spider))
clock = pygame.time.Clock()

while 1:
	clock.tick(60)
	"""
	for event in pygame.event.get():
		if event.type == QUIT:
		
		elif event.type == KEYDOWN and event.key == K_ESCAPE:
			
		elif event.type == MOUSEBUTTONDOWN:
			
		elif event.type == MOUSEBUTTONUP:
	"""
	allsprites.update()
	screen.blit(background, (0, 0))
	allsprites.draw(screen)
	pygame.display.flip()
